package com.irdstudio.efp.esb.service.impl.sed.repay;

import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.service.bo.resp.sed.repay.RespLoanRepayBean;
import com.irdstudio.efp.esb.service.client.ESBRequestBuilder;
import com.irdstudio.efp.esb.service.client.EsbBaseBean;
import com.irdstudio.efp.loan.service.vo.LoanRepayDetailVO;
import com.irdstudio.efp.nls.common.exception.RepayException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.bo.req.sed.repay.ReqLoanRepayBean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.facade.sed.repay.LoanRepayService;

/**
* 普税贷产品对税e贷产品的此部分代码做了复用
* 说明：为了保证功能的复用性、提升开发的工作效率，对该代码进行了复用
* 评估是否会影响线上功能：否
* 评估时间：2020/5/25
* 处理方案：保留复用
*/
/**
 * 需求编号：【2019D0519】（业务提的需求编号）
 * 问题编号：【xxxxxxx】（若不是因为修复问题而新增该类，则留空）
 * 开发人员：qiuyf
 * 创建日期：2019年5月7日 上午9:16:24
 * 功能描述：贷款还款接口实现（ --> 互金）
 * 	30210002(贷款还款)
 * 	01(贷款结算还款)
 */
@Service("loanRepayService")
public class LoanRepayServiceImpl implements LoanRepayService {

	private static Logger logger = LoggerFactory.getLogger(LoanRepayServiceImpl.class);

	@Override
	public RespLoanRepayBean loanRepay(ReqLoanRepayBean reqLoanRepayBean, LoanRepayDetailVO loanRepayDetailVO) throws RepayException {
		logger.info("======>调用贷款还款接口 全局流水号【"+ loanRepayDetailVO.getGlobalSerno() +"_30210002_01】开始<======");
		RespLoanRepayBean respLoanRepayBean = new RespLoanRepayBean();
		EsbRespServiceBean  esbRespServiceBean = new EsbRespServiceBean();
		try {
			EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.anEsbBaseBean().
					withGLBLSRLNO(loanRepayDetailVO.getGlobalSerno()).
					withINSTID(ESBClientConstance.bdInstId).
					withPLATFMID(loanRepayDetailVO.getPlatfmId()).build();

			esbRespServiceBean = ESBRequestBuilder.buildClient()
					.withEsbBaseBean(esbBaseBean)
					.withBody(reqLoanRepayBean)
					.withTradeNo(ESBConstant.TRADE_CODE_30210002)
					.withScene(ESBConstant.SCENE_NO_01)
					.create()
					.sendAndReceive();

			if(!EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(esbRespServiceBean.getSysHead().getRetInfArry().get(0).getRetCd())){
				throw new RepayException(esbRespServiceBean.getSysHead().getRetInfArry().get(0).getRetMsg());
			}
			respLoanRepayBean = esbRespServiceBean.getBody(RespLoanRepayBean.class);
			respLoanRepayBean.setSysHeadRetCode(esbRespServiceBean.getSysHead().getRetInfArry().get(0).getRetCd());
			logger.info("调用贷款还款接口【30210002_01】结束，返回的系统头信息：" + JSONObject.toJSONString(esbRespServiceBean.getSysHead()));
		} catch (Exception e) {
			logger.error("调用贷款还款接口【30210002_01】出现异常：" + e.getMessage());
			throw new RepayException(e.getMessage());
		} finally {
			logger.info("======>调用贷款还款接口【"+ loanRepayDetailVO.getGlobalSerno() +"_30210002_01】结束<======");
		}
		return respLoanRepayBean;
	}

}
